﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebUI.DAL;
using System.Data.Linq;

namespace WebUI.BL
{
    public class ShopProductBL
    {
        public void Insert(tbl_ShopProduct ShopProduct)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                try
                {
                    tbl_ShopProduct newShopProduct = new tbl_ShopProduct
                    {
                        ProductNo = ShopProduct.ProductNo,
                        ShopNo = ShopProduct.ShopNo,
                        Status = ShopProduct.Status,
                        CreatedUser = ShopProduct.CreatedUser,
                        CreatedDate = ShopProduct.CreatedDate
                    };
                    contextdb.tbl_ShopProducts.InsertOnSubmit(newShopProduct);
                    contextdb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }
        public void Update(tbl_ShopProduct ShopProduct)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var updateShopProduct = contextdb.tbl_ShopProducts.SingleOrDefault(u => u.ShopNo == ShopProduct.ShopNo&&u.ProductNo==ShopProduct.ProductNo);
                try
                {
                    if (updateShopProduct != null)
                    {
                        updateShopProduct.ProductNo = ShopProduct.ProductNo;
                        updateShopProduct.ShopNo = ShopProduct.ShopNo;
                        updateShopProduct.Status = ShopProduct.Status;
                        updateShopProduct.CreatedDate = ShopProduct.CreatedDate;
                        updateShopProduct.CreatedUser = ShopProduct.CreatedUser;

                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }

            }
        }
        public void Delete(int ProductNo, int ShopNo)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var deleteShopProduct = contextdb.tbl_ShopProducts.SingleOrDefault(u => u.ProductNo == ProductNo&&u.ShopNo==ShopNo);
                try
                {
                    if (deleteShopProduct != null)
                    {
                        contextdb.tbl_ShopProducts.DeleteOnSubmit(deleteShopProduct);
                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }
    }
}
